package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.ExhibitorFeign;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.ExhibitionServiceProviderDTO;
import com.eastfair.venueservice.dto.ExhibitionServiceProviderPageQuery;
import com.eastfair.venueservice.dto.ExhibitionServiceProviderSaveDTO;
import com.eastfair.venueservice.dto.ExhibitionServiceProviderUpdateDTO;
import com.eastfair.venueservice.entity.ExhibitionServiceProvider;
import com.eastfair.venueservice.service.ExhibitionServiceProviderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 展会服务商关系
 * </p>
 *
 * @author linan
 * @date 2022-07-15
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/exhibitionServiceProvider")
@Api(value = "ExhibitionServiceProvider", tags = "展会服务商关系")
@PreAuth(replace = "venueservice:exhibitionServiceProvider:")
public class ExhibitionServiceProviderController extends SuperController<ExhibitionServiceProviderService, Long, ExhibitionServiceProvider, ExhibitionServiceProviderPageQuery, ExhibitionServiceProviderSaveDTO, ExhibitionServiceProviderUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ExhibitionServiceProviderService exhibitionServiceProviderService;

    @Override
    public void handlerResult(IPage<ExhibitionServiceProvider> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ExhibitionServiceProvider> exhibitionServiceProviderList = list
                .stream()
                .map((map) -> ExhibitionServiceProvider.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(exhibitionServiceProviderList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ExhibitionServiceProviderPageQuery> params) {
        ExhibitionServiceProviderPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 保存展会服务商关系.
     *
     * @param exhibitionServiceProviderDTO the exhibition service provider dto
     * @return the r
     */
    @ApiOperation("保存展会服务商关系")
    @PostMapping("/save")
    public R<Boolean> saveExhibitionServiceProvider(@RequestBody ExhibitionServiceProviderDTO exhibitionServiceProviderDTO) {
        log.info("saveExhibitionServiceProvider - 保存展会服务商关系, exhibitionServiceProviderDTO={}", exhibitionServiceProviderDTO);
        try {
            return R.successDef(exhibitionServiceProviderService.saveExhibitionServiceProvider(exhibitionServiceProviderDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 删除展会下的服务商关系.
     *
     * @param exhibitionServiceProviderDTO the exhibition service provider dto
     * @return the r
     */
    @ApiOperation("删除展会下的服务商关系")
    @PostMapping("/remove/serviceProvider")
    public R<Boolean> deleteByServiceProviderId(@RequestBody ExhibitionServiceProviderDTO exhibitionServiceProviderDTO) {
        log.info("deleteByServiceProviderId - 删除展会下的服务商关系, exhibitionServiceProviderDTO={}", exhibitionServiceProviderDTO);
        try {
            return R.successDef(exhibitionServiceProviderService.removeByServiceProvider(exhibitionServiceProviderDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
